package com.share.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

public class PinYinUtil {
    /**
     * 汉语拼音格式化工具类
     */
    private static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

    static {
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
        format.setVCharType(HanyuPinyinVCharType.WITH_V);// v:的声母
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不写这一句,则默认添加声调的
    }


    /**
     * 获取字符串内的所有汉字的汉语拼音
     *
     * @param src 智宇数字
     * @return string --> Zhi Yu Shu Zi
     */
    public static String spell(String src) {
        return spell_all_base(src, false);
    }

    public static String spellSkipAlpha(String src) {
        return spell_all_base(src, true);
    }

    public static String spellFirstChar(String src) {
        return spell_first_base(src, false);
    }

    public static String spellFirstCharSkipAlpha(String src) {
        return spell_first_base(src, true);
    }

    // 返回空格切割的 拼音
    // eg  你好 11 2女
    // 返回  ni hao 11 2 nu
    private static String spell_first_base(String src, boolean isFilterHanZi) {
        if (StringUtils.isBlank(src)) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        int length = src.length();
        Boolean isHanZi = null;
        try {
            for (int i = 0; i < length; i++) {
                // 对英文字母的处理：小写字母转换为大写，大写的直接返回
                char c = src.charAt(i);
                if (isHanZi(c)) {
                    String[] tmpPy = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    char fc = tmpPy[0].charAt(0);//汉字存在多音字


                    if (isHanZi == null) {
                        sb.append(fc);
                    } else {
                        if (isHanZi) {
                            sb.append(fc);
                        } else {
                            sb.append(" ");
                            sb.append(fc);
                        }
                    }

                    isHanZi = true;
                } else {
                    if (isFilterHanZi) {
                        //跳过字符的处理
                    } else {
                        if (isHanZi == null) {
                            sb.append(c);
                        } else if (isHanZi) {
                            sb.append(" ");
                            sb.append(c);
                        } else {
                            sb.append(c);
                        }

                        isHanZi = false;
                    }
                }
            }

            return sb.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String spell_all_base(String src, boolean isFilterHanZi) {
        if (StringUtils.isBlank(src)) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        int length = src.length();
        Boolean isHanZi = null;
        try {
            for (int i = 0; i < length; i++) {
                // 对英文字母的处理：小写字母转换为大写，大写的直接返回
                char c = src.charAt(i);
                if (isHanZi(c)) {
                    String[] tmpPy = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    String py = tmpPy[0];//汉字存在多音字

                    if (isHanZi == null) {
                        sb.append(py);
                    } else {
                        sb.append(" ");
                        sb.append(py);
                    }

                    isHanZi = true;
                } else {
                    if (isFilterHanZi) {
                        //跳过字符的处理
                    } else {
                        if (isHanZi == null) {
                            sb.append(c);
                        } else if (isHanZi) {
                            sb.append(" ");
                            sb.append(c);
                        } else {
                            sb.append(c);
                        }

                        isHanZi = false;
                    }
                }
            }

            return sb.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
            return null;
        }
    }

    //判断字符是否是汉字
    private static boolean isHanZi(char c) {
        if ((c >= '\u4E00' && c <= '\u9FA5') || (c >= '\uF900' && c <= '\uFA2D')) {
            return true;
        } else {
            return false;
        }
    }

    //全部都是汉字连接
    public static boolean isOnlyHanZi(String src) {
        boolean res = true;
        if (StringUtils.isNoneBlank(src)) {
            int length = src.length();
            for (int i = 0; i < length; i++) {
                char c = src.charAt(i);
                if (!isHanZi(c)) {
                    res = false;
                    break;
                }
            }
        } else {
            res = false;
        }
        return res;
    }

    //是否包含汉字
    public static boolean isContainHanZi(String src) {
        boolean res = false;
        if (StringUtils.isNoneBlank(src)) {
            int length = src.length();
            for (int i = 0; i < length; i++) {
                char c = src.charAt(i);
                if (isHanZi(c)) {
                    res = true;
                    break;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        String str = "你 ";
        System.out.println(isOnlyHanZi(str));
        System.out.println(spell(str));
        System.out.println(spellSkipAlpha(str));

        System.out.println(spellFirstChar(str));
        System.out.println(spellFirstCharSkipAlpha(str));
    }
}
